1029A - Many Equal Substrings - CodeForces Solution


implementation strings *1300

Please click on ads to support us..

Python Code:

n, k = map(int, input().split())
t = input()
x = 0
for i in range(1, n):
  if t[:i] == t[-i:]:
    x = i
t += t[x:] * (k - 1)
print(t)

C++ Code:

#include <bits/stdc++.h>

using namespace std ;

typedef  long long  ll ;
#define  test int t;cin>>t;while(t--)
#define fast() {ios::sync_with_stdio(0) ; cin.tie(0); cout.tie(0);}
int mod = 1e9 + 7;
// int n, a[16][16];
// ll dp[16][1<<16];
// ll solve(int i, int mask)
// {
//   // cout << i << ' ' << mask << endl;
//     if(i == n) return 0;
//     ll &ret = dp[i][mask];
//     if(~ret) return ret;
//     ll sum = 0;
//     for(int j = 0; j < i; j++){
//         if(mask & (1 << j)) sum += a[j][i];
//     }
//     ll path1 = 0, path2 = 0;
//     path1 += solve(i + 1, mask | (1 << i)) + sum;
//     path2 += solve(i + 1, mask);
//     return ret = max(path1, path2);
// }

int main()
{
    fast();
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    string a = "", b;
    int j = -1;
    for(int i = 0; i < n - 1; i++){
        a += s[i];
        b = s.substr(n - i - 1, i + 1);
        if(a == b) j = i;
    }
    a = s.substr(j + 1, n - (j + 1));
    cout << s;
    while(k-- > 1) cout << a;
    return 0;
}


Comments

Submit
0 Comments
More Questions

1670D - Very Suspicious
1141B - Maximal Continuous Rest
1341A - Nastya and Rice
1133A - Middle of the Contest
385A - Bear and Raspberry
1311B - WeirdSort
1713F - Lost Array
236B - Easy Number Challenge
275A - Lights Out
147A - Punctuation
253A - Boys and Girls
1327E - Count The Blocks
984A - Game
12B - Correct Solution
1355B - Young Explorers
485A - Factory
628A - Tennis Tournament
1436B - Prime Square
1707B - Difference Array
1422C - Bargain
1611F - ATM and Students
660A - Co-prime Array
1692F - 3SUM
1470A - Strange Birthday Party
190D - Non-Secret Cypher
1721B - Deadly Laser
1721C - Min-Max Array Transformation
1721A - Image
1180C - Valeriy and Deque
557A - Ilya and Diplomas